let util = require('../../../../../common/util/util')

const app = getApp()

let innerAudioContext = wx.createInnerAudioContext();

Page({
    data: {
        ms: [],
        scrollTop: 0,
        anmwidth: 100,
        height: 100,
        plc: '请输入问题' ,
        bottom: 0,
        xUserInfo: '',
        scrollto:'',
        endId:'',
        heightS:'',
        uava:'',
        id:''
    },
    onShareAppMessage: function () {
      return {
        title: '疼痛久治不愈？还有一招',
        imageUrl: 'https://7633-v3-92ah4-1300208657.tcb.qcloud.la/static/share_all.jpg',
      };
    },
    onLoad: function (opt) {
        let that = this
        that.setData({
            id: opt.id,
            name: opt.name,
            // ava: opt.ava,
            uava: opt.ava
        })
        wx.setNavigationBarTitle({
            title: opt.name ,
        })
    },

    onShow: function () {
        this.xInit()
        // this.xMessaged()
        // wx.getSystemInfo({
        //   success: (e) => {
        //     this.setData({
        //       heightS:e.screenHeight-180 +'px'
        //     })
        //     console.log(e.screenHeight)
        //   }
        // })
    },
    autoScroll : function(){
        let view_id = 'view_id'+ Math.floor(Math.random()*100000);
        this.setData({
          scrollto:''
        })
        this.setData({
          endId:view_id
        })
        this.setData({
          scrollto:view_id
        })
    },
    xInit: function () {
        let that = this
        let id = that.data.id
        let _dataObj = {
            object_openid: id
        }
        that.setData({
            value : '' ,
            anmwidth : 100 ,
            anmheight : 60 ,
        })

        wx.cloud.callFunction({
            name: 'user',
            data: {
                action: 'detail'
            }
        }).then(res => {
            let user = res.result
            that.setData({
                xUserInfo: user
            })
        })
        
        wx.cloud.callFunction({
            name: 'consulting_message',
            data: {
                action: "get_oneOnone_messages",
                dataObj: _dataObj
            }
        }).then(res => {
            console.log("消息:", res)
            let data = res.result
            for (var i in data) {
              data[0].quote_info[0].istime=true
              if(i>0){
                var num1=Number(Math.round(new Date(data[Number(i)-Number(1)].quote_info[0].create_time).getTime()/1000).toString());
                var num2=Number(Math.round(new Date(data[i].quote_info[0].create_time).getTime()/1000).toString());
                var m = parseInt((num2-num1)/(60))
                if(m>10){
                  data[i].quote_info[0].istime=true
                }
              }
            }
            for (var i in data) {
              if(util.times(data[i].quote_info[0].create_time)==-1){
                data[i].quote_info[0].create_time = util.formatTime_chineseTime(new Date(data[i].quote_info[0].create_time))
              }else if(util.times(data[i].quote_info[0].create_time)==0){
                data[i].quote_info[0].create_time = '昨天'+util.formatTime_chineseTime(new Date(data[i].quote_info[0].create_time))
              }else{
                data[i].quote_info[0].create_time = util.formatTime_emotion(new Date(data[i].quote_info[0].create_time))
              }
            }
            that.setData({
                ms: data
            })
            that.autoScroll()
        })
    },
    // 查看图片并保存
    seeImg:function(e){
      let url=e.currentTarget.dataset.url
      let list =[]
      list.push(url)
      wx.previewImage({
        current: url, // 当前显示图片的 http 链接
        urls: list // 需要预览的图片 http 链接列表
      })
    },
    // xToWrite: function () {
    //     let that = this
    //     let id = that.data.id
    //     let name = that.data.name
    //     let ava = that.data.ava
    //     wx.navigateTo({
    //         url: '/doc/pages/chat/user/quiz/index?id=' + id + '&name=' + name + '&ava=' + ava,
    //     })
    // },

    player: function (e) {
        let that = this
        let params = e.currentTarget.dataset
        let data = that.data.ms
        if (data[params.index].quote_info[0].content.content.audio === undefined) {
            data[params.index].quote_info[0].content.content.text.record = false
        } else {
            data[params.index].quote_info[0].content.content.audio.record = false
        }
        // if()
        that.setData({
            playStatus: false,
            ms: data
        })
        var musicUrl = params.vocd
        console.log(params)
        console.log('musicUrl', musicUrl)
        innerAudioContext.src = musicUrl;
        innerAudioContext.autoplay = true
        innerAudioContext.play();
        innerAudioContext.onEnded(() => {
            that.setStopState(params)
        })
    },

    // xMessaged: function () {
    //     let that = this
    //     let id = that.data.id
    //     let _dataObj = {
    //         from_openid: id
    //     }
    //     wx.cloud.callFunction({
    //         name: 'consulting_message',
    //         data: {
    //             action: "update_message_read_status",
    //             dataObj: _dataObj
    //         }
    //     }).then(res => {
    //         console.log("返回数据:", res)
    //     })
    // },

    setStopState: function (params) {
        let that = this
        let data = that.data.ms
        if (data[params.index].quote_info[0].content.content.audio === undefined) {
            data[params.index].quote_info[0].content.content.text.record = true
        } else {
            data[params.index].quote_info[0].content.content.audio.record = true
        }
        that.setData({
            playStatus: false,
            ms: data
        })
    },

    xToProduct: function () {
        wx.navigateTo({
            url: '/subsale/pages/product/addsvc/index?id=617ef50c622575a00a1ea51026fc3837',
        })
    },

    xScrollBottom: function () {
        let that = this
        wx.createSelectorQuery().select('#ownContainer').boundingClientRect(function (rect) {
            wx.pageScrollTo({
                scrollTop: rect.height,
                duration: 100
            })
            that.setData({
                scrollTop: rect.height - that.data.scrollTop
            });
        }).exec();
    },

    inputfocus: function () {
        let that = this
        that.setData({
            height: 800,
            plc: ''
        })
    },

    tapinput: function () {
        let that = this
        // 你可以随时向咨询师提问，请尽量将问题描述的明确、全面，以便咨询师给到您合适的回复/
        that.setData({
            height: 800,
            plc: ''
        })
    },

    inputblur: function (e) {
        let that = this
        that.setData({
          height: 100,
          plc: '请输入问题'
        })
    },

    xClear: function () {
        let that = this
        that.setData({
            anmwidth : 100 ,
            anmheight : 60 ,
            value : ''
        })
    },

    keybord: function(e){
        console.log(e)
        let that = this
        let height = e.detail.height
        that.setData({
            bottom : height
        })
    },

    input: function (e) {
        let that = this
        let value = e.detail.value
        that.setData({
            value: value
        })
    },

    xSub: function () {
        let that = this
        let value = that.data.value
        let id = that.data.id
        if(value === ''){
            wx.showToast({
                title: '请输入内容后提交',
                icon: 'none'
            })
        }else{
          let _dataObj ={
            to_openid: id,
            message_type: 1, // 1:文字
            business_type: 6, // 6:咨询师回复
            content: {
              title: value, //标题
              content:{
                   pic: "", //图片
                   text: value, //文本
                   audio: "", //语音
                   parameter: "" //参数，根据业务类型和参数做对应的跳转
               }
            }
        }
        wx.cloud.callFunction({
            name: 'consulting_message',
            data: {
                action: "send_message",
                dataObj: _dataObj
            }
        }).then(res => {
            that.xInit()
            // wx.showToast({
            //     title: '发送成功',
            //     icon: 'none',
            //     success : () => {
            //         setTimeout( () => {
            //             wx.navigateBack({
            //                 delta: -1,
            //             })
            //         } , 1500)
            //     }
            // })
        })
        }
    }

})